using CollabApp.Domain.Entities.Game;

namespace CollabApp.Domain.Services.Game;

/// <summary>
/// 动态平衡服务接口
/// 负责实现游戏的动态平衡机制，包括领先玩家debuff、落后玩家buff等
/// 确保游戏的公平性和趣味性
/// </summary>
public interface IDynamicBalanceService
{
    /// <summary>
    /// 计算玩家的动态平衡修正值
    /// </summary>
    /// <param name="gameId">游戏ID</param>
    /// <param name="playerId">玩家ID</param>
    /// <returns>平衡修正结果</returns>
    Task<BalanceModifierResult> CalculateBalanceModifiersAsync(Guid gameId, Guid playerId);

    /// <summary>
    /// 检查是否需要启用橡皮筋机制（临时联盟）
    /// </summary>
    /// <param name="gameId">游戏ID</param>
    /// <returns>橡皮筋机制检查结果</returns>
    Task<RubberBandResult> CheckRubberBandMechanismAsync(Guid gameId);

    /// <summary>
    /// 应用动态平衡效果
    /// </summary>
    /// <param name="gameId">游戏ID</param>
    /// <param name="playerId">玩家ID</param>
    /// <param name="modifiers">平衡修正值</param>
    /// <returns>应用结果</returns>
    Task<bool> ApplyBalanceModifiersAsync(Guid gameId, Guid playerId, BalanceModifierResult modifiers);

    /// <summary>
    /// 获取所有玩家的平衡状态
    /// </summary>
    /// <param name="gameId">游戏ID</param>
    /// <returns>平衡状态列表</returns>
    Task<List<PlayerBalanceStatus>> GetAllPlayersBalanceStatusAsync(Guid gameId);

    /// <summary>
    /// 更新游戏平衡状态
    /// </summary>
    /// <param name="gameId">游戏ID</param>
    /// <returns>更新结果</returns>
    Task<BalanceUpdateResult> UpdateGameBalanceAsync(Guid gameId);

    /// <summary>
    /// 清除所有平衡效果
    /// </summary>
    /// <param name="gameId">游戏ID</param>
    /// <returns>清除结果</returns>
    Task<bool> ClearAllBalanceEffectsAsync(Guid gameId);
}

/// <summary>
/// 平衡修正结果
/// </summary>
public class BalanceModifierResult
{
    /// <summary>
    /// 操作是否成功
    /// </summary>
    public bool Success { get; set; }

    /// <summary>
    /// 玩家ID
    /// </summary>
    public Guid PlayerId { get; set; }

    /// <summary>
    /// 玩家排名（1为第一名）
    /// </summary>
    public int PlayerRank { get; set; }

    /// <summary>
    /// 玩家领地面积百分比
    /// </summary>
    public float TerritoryPercentage { get; set; }

    /// <summary>
    /// 移动速度修正值（1.0为正常，>1.0为加速，<1.0为减速）
    /// </summary>
    public float SpeedModifier { get; set; } = 1.0f;

    /// <summary>
    /// 道具效果增强倍率（1.0为正常）
    /// </summary>
    public float PowerUpEffectMultiplier { get; set; } = 1.0f;

    /// <summary>
    /// 是否为领先玩家（受到debuff）
    /// </summary>
    public bool IsLeadingPlayer { get; set; }

    /// <summary>
    /// 是否为落后玩家（获得buff）
    /// </summary>
    public bool IsLaggingPlayer { get; set; }

    /// <summary>
    /// 平衡效果类型
    /// </summary>
    public BalanceEffectType EffectType { get; set; }

    /// <summary>
    /// 效果描述
    /// </summary>
    public string EffectDescription { get; set; } = string.Empty;

    /// <summary>
    /// 效果持续时间（秒）
    /// </summary>
    public int EffectDurationSeconds { get; set; }

    /// <summary>
    /// 错误信息
    /// </summary>
    public List<string> Errors { get; set; } = new();

    /// <summary>
    /// 提示信息
    /// </summary>
    public List<string> Messages { get; set; } = new();
}

/// <summary>
/// 橡皮筋机制结果
/// </summary>
public class RubberBandResult
{
    /// <summary>
    /// 是否需要启用橡皮筋机制
    /// </summary>
    public bool ShouldActivate { get; set; }

    /// <summary>
    /// 主导玩家ID（领地面积超过40%的玩家）
    /// </summary>
    public Guid? DominantPlayerId { get; set; }

    /// <summary>
    /// 主导玩家名称
    /// </summary>
    public string? DominantPlayerName { get; set; }

    /// <summary>
    /// 主导玩家领地面积百分比
    /// </summary>
    public float DominantPlayerAreaPercentage { get; set; }

    /// <summary>
    /// 联盟玩家ID列表（除主导玩家外的其他玩家）
    /// </summary>
    public List<Guid> AlliancePlayerIds { get; set; } = new();

    /// <summary>
    /// 联盟buff效果描述
    /// </summary>
    public string AllianceBuffDescription { get; set; } = string.Empty;

    /// <summary>
    /// 联盟持续时间（秒）
    /// </summary>
    public int AllianceDurationSeconds { get; set; }

    /// <summary>
    /// 提示信息
    /// </summary>
    public List<string> Messages { get; set; } = new();
}

/// <summary>
/// 玩家平衡状态
/// </summary>
public class PlayerBalanceStatus
{
    /// <summary>
    /// 玩家ID
    /// </summary>
    public Guid PlayerId { get; set; }

    /// <summary>
    /// 玩家名称
    /// </summary>
    public string PlayerName { get; set; } = string.Empty;

    /// <summary>
    /// 当前排名
    /// </summary>
    public int CurrentRank { get; set; }

    /// <summary>
    /// 领地面积百分比
    /// </summary>
    public float TerritoryPercentage { get; set; }

    /// <summary>
    /// 当前生效的平衡效果
    /// </summary>
    public List<ActiveBalanceEffect> ActiveEffects { get; set; } = new();

    /// <summary>
    /// 是否在橡皮筋联盟中
    /// </summary>
    public bool InRubberBandAlliance { get; set; }

    /// <summary>
    /// 平衡状态类型
    /// </summary>
    public PlayerBalanceType BalanceType { get; set; }

    /// <summary>
    /// 状态更新时间
    /// </summary>
    public DateTime LastUpdateTime { get; set; }
}

/// <summary>
/// 活跃平衡效果
/// </summary>
public class ActiveBalanceEffect
{
    /// <summary>
    /// 效果ID
    /// </summary>
    public string EffectId { get; set; } = string.Empty;

    /// <summary>
    /// 效果类型
    /// </summary>
    public BalanceEffectType EffectType { get; set; }

    /// <summary>
    /// 效果值
    /// </summary>
    public float EffectValue { get; set; }

    /// <summary>
    /// 开始时间
    /// </summary>
    public DateTime StartTime { get; set; }

    /// <summary>
    /// 剩余时间（秒）
    /// </summary>
    public int RemainingSeconds { get; set; }

    /// <summary>
    /// 效果描述
    /// </summary>
    public string Description { get; set; } = string.Empty;

    /// <summary>
    /// 是否活跃
    /// </summary>
    public bool IsActive { get; set; } = true;
}

/// <summary>
/// 平衡更新结果
/// </summary>
public class BalanceUpdateResult
{
    /// <summary>
    /// 更新是否成功
    /// </summary>
    public bool Success { get; set; }

    /// <summary>
    /// 更新的玩家数量
    /// </summary>
    public int UpdatedPlayersCount { get; set; }

    /// <summary>
    /// 新激活的效果数量
    /// </summary>
    public int NewEffectsCount { get; set; }

    /// <summary>
    /// 过期的效果数量
    /// </summary>
    public int ExpiredEffectsCount { get; set; }

    /// <summary>
    /// 是否激活了橡皮筋机制
    /// </summary>
    public bool RubberBandActivated { get; set; }

    /// <summary>
    /// 橡皮筋主导玩家ID
    /// </summary>
    public Guid? RubberBandDominantPlayer { get; set; }

    /// <summary>
    /// 更新详情
    /// </summary>
    public List<string> UpdateDetails { get; set; } = new();

    /// <summary>
    /// 错误信息
    /// </summary>
    public List<string> Errors { get; set; } = new();

    /// <summary>
    /// 更新时间
    /// </summary>
    public DateTime UpdateTime { get; set; } = DateTime.UtcNow;
}

/// <summary>
/// 平衡效果类型
/// </summary>
public enum BalanceEffectType
{
    /// <summary>
    /// 无效果
    /// </summary>
    None = 0,

    /// <summary>
    /// 领先玩家减速debuff
    /// </summary>
    LeaderSpeedDebuff = 1,

    /// <summary>
    /// 落后玩家加速buff
    /// </summary>
    LagginPlayerSpeedBuff = 2,

    /// <summary>
    /// 落后玩家道具增强
    /// </summary>
    LaggingPlayerPowerUpBuff = 3,

    /// <summary>
    /// 橡皮筋联盟buff
    /// </summary>
    RubberBandAllianceBuff = 4,

    /// <summary>
    /// 橡皮筋联盟轨迹免疫
    /// </summary>
    RubberBandTrailImmunity = 5
}

/// <summary>
/// 玩家平衡类型
/// </summary>
public enum PlayerBalanceType
{
    /// <summary>
    /// 平衡状态
    /// </summary>
    Balanced = 0,

    /// <summary>
    /// 领先玩家（受到debuff）
    /// </summary>
    Leading = 1,

    /// <summary>
    /// 落后玩家（获得buff）
    /// </summary>
    Lagging = 2,

    /// <summary>
    /// 主导玩家（触发橡皮筋机制）
    /// </summary>
    Dominant = 3,

    /// <summary>
    /// 联盟成员（橡皮筋机制中的其他玩家）
    /// </summary>
    AllianceMember = 4
}
